package base.annotation_demo.demo_02;

import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 *
 * 1. testAnnotation()演示根据反射获得类、属性上的注解
 *
 * 2. testORM() 简单演示如何将Java对象转换成sql查询数据库
 *
 *
 */
@Slf4j
public class Test {
    public static void main(String[] args) throws Exception {
//        testAnnotation();
        testORM();
    }

    public static void testAnnotation() throws ClassNotFoundException, NoSuchFieldException {
        Class<Student> clazz = (Class<Student>) Class.forName("base.annotation_demo.demo_02.Student");

        // 获取Student类上所有注解
        Annotation[] annotations = clazz.getAnnotations();
        
        for (Annotation annotation : annotations) {
            log.info("ALL: {}", annotation);
        }

        // 获取类上指定注解
        TabAnnotation annotation = clazz.getAnnotation(TabAnnotation.class);
        log.info("{}", annotation.value());
        // 获取类的属性注解
        Field field = clazz.getDeclaredField("name");
        FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
        log.info("{} -> {} -> {}", fieldAnnotation.columnName(), fieldAnnotation.type(), fieldAnnotation.length());
    }

    public static void testORM() throws IllegalAccessException {
        Student student = new Student();
        student.setId(1000);
        student.setName("张三");
        student.setAge(20);

        String sql = ORM.querySQL(student);
        log.info("SQL -> {}", sql);
    }
}
